package com.ml.dao.impl;

import com.ml.dao.BaseDaoBak;
import com.ml.dao.DkDao;
import com.ml.model.Dk;
import com.ml.model.KqData;
import com.ml.util.UtilMonthMaxDay;
import com.ml.util.UtilProperties;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 查看打卡的实现dao
 */
public class DkDaoImpl extends BaseDaoBak implements DkDao {


    public List<Dk> getAllBook() {
        String sql = "SELECT * FROM users";
        List<Dk> list = new ArrayList<Dk>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = this.openConnect();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                Dk u = new Dk();//每一个实体生成一个对象存到list

                //添加到list中去
                list.add(u);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(conn!=null){
                this.closeConnect(conn, null, ps, rs);
            }
        }
        return list;
    }


    @Override
    /**
     *
     */
    public List<String> getDkRecordString(String startdate, String enddate, String user) {
        // access  支持这种格式的时间 2019/6/01 06:59:59
        String sql = "SELECT b.NAME , a.CHECKTIME  FROM CHECKINOUT a , USERINFO b   " +
                "where a.userid in ( select userid  from USERINFO where name='" + user + "')  " +
                "and a.userid = b.userid   " +
                "and CHECKTIME>=cdate('" + startdate + "') and  cdate(CHECKTIME)<=cdate('" + enddate + "') " +
                "ORDER BY  a.checktime";
        List<String> list = new ArrayList<String>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = this.openConnect();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                //添加到list中去
                String checktime = rs.getString("CHECKTIME");
                list.add(checktime);
            }
            this.closeConnect(conn, null, ps, rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }


    public List<Dk> getDkRecord(String startdate, String enddate, String user) {

        List<String> list = getDkRecordString(startdate + "/01 00:00:00", enddate + "/01 00:00:00", user);
        List<Dk> list2 = new ArrayList<Dk>();//"2019/5/01 00:00:00" "2019/6/01 00:00:00"
        //从开始的时间里，获取该月份
        String year = startdate.split("/")[0];
        String month = startdate.split("/")[1];
        int maxDay = UtilMonthMaxDay.getMaxDayByYearMonth(Integer.valueOf(year), Integer.valueOf(month));//获取当月最大天数
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 1; i <= maxDay; i++) {
            try {
                List todayDk = new ArrayList();    //获取一天中所有的打开数据，
                Dk dk = new Dk();
                String d0 = i > 9 ? i + "" : "0" + i;
                int m = Integer.valueOf(month);
                String m0 = m > 9 ? (m + "") : ("0" + m);
                String date0 = year + "-" + m0 + "-" + d0;//获取时间格式为 2019-05-07
                //String date1 = year+"/"+ m0 +"/"+d0;//获取时间格式为 2019/05/07
                //获取星期几
                String week = UtilMonthMaxDay.getWeekDay(sdf.parse(date0+" 00:00:00"));
                if(week.contains("六") || week.contains("日")){
                    dk.setZm(1);//周末
                }
                dk.setDate(date0+" "+week );//日期列表
                dk.setUser(user);//人
                //获取当天的打卡数据
                if (list != null && list.size() > 0) {
                    for (String dateStr : list) {
                        int dday = Integer.valueOf(dateStr.substring(8, 10));
                        if (dday == i) {
                            todayDk.add(dateStr);
                        }
                    }
                }
                //  todayDk 是一天的所有打卡数据，按照事假循序排序了
                //获取一天中所有的打开数据，
                if (todayDk != null && todayDk.size() > 0) {
                    Object a[] = (Object[]) todayDk.toArray();
                    Object first = a[0];
                    Object last = a[0];//打卡来一次
                    if (todayDk.size() > 1) {//多次
                        last = a[a.length - 1];
                    }

                    //比较数据是否，迟到和早退
                    String sbStr = date0 + " " + UtilProperties.getConfig("dk.sb");
                    String xbStr = date0 + " " + UtilProperties.getConfig("dk.xb");
                    Date sb = sdf.parse(sbStr);//标准上班时间
                    Date xb = sdf.parse(xbStr);//标准下班班时间
                    Date d1 = sdf.parse(first.toString());//实际上班打卡时间
                    Date d2 = sdf.parse(last.toString());//实际下班打卡时间
                    dk.setStartTime(first.toString());
                    dk.setEndTime(last.toString());
                    if (d1.getTime() > sb.getTime()) {//判断是否迟到
                        dk.setChiDao(1);//迟到
                    }

                    if (d2.getTime() < xb.getTime()) {//判断是否早退
                        dk.setZaoTui(1);//早退
                    }

                } else {//没有获取到当天的数据
                    dk.setChiDao(1);
                    dk.setZaoTui(1);
                    dk.setDkf(1);//没有数据未打卡
                }
                list2.add(dk);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        return list2;
    }


    /** 获取指定日期泛微内的 所有打卡记录 */
    public List<KqData> getKqDataDate(String startdate, String enddate){
        startdate = startdate + " 00:00:00";
        enddate = enddate + " 23:59:59";
        List<KqData> list = new ArrayList<KqData>();//"2019/5/01 00:00:00" "2019/6/01 00:00:00"
        // access  支持这种格式的时间 2019/6/01 06:59:59
        String sql = "SELECT b.NAME  , a.CHECKTIME   FROM CHECKINOUT a , USERINFO b   " +
                "where a.userid in ( select userid  from USERINFO where  " +
                " a.userid = b.userid )  " +
                "and CHECKTIME>=cdate('" + startdate + "') and  cdate(CHECKTIME)<=cdate('" + enddate + "') " +
                "ORDER BY  a.checktime";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = this.openConnect();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                //添加到list中去
                String checktime = rs.getString("CHECKTIME");
                String name = rs.getString("NAME");
                String signDate = "";
                String signTime = "";
                if(checktime!=null && !"".equals(checktime)){
                    KqData o = new KqData();
                    String times[] = checktime.split(" ");
                    signDate = times[0];
                    signTime = times[1];
                    o.setBelongdate(signDate);
                    o.setSignDate(signDate);
                    o.setSignTime(signTime);
                    o.setUserName(name);
                    list.add(o);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(conn!=null){
                this.closeConnect(conn, null, ps, rs);
            }
        }

        return list;
    }

    @Override
    public List<Dk> getTodayDkMin(String startdate, String enddate, String user) {
        return null;
    }

    @Override
    public List<Dk> getTodayDkMax(String startdate, String enddate, String user) {
        return null;
    }
}
